package com.one.common.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

public class LayoutUtils {
	public static final int MATCH = ViewGroup.LayoutParams.MATCH_PARENT;
	public static final int WRAP = ViewGroup.LayoutParams.WRAP_CONTENT;

	/**
	 * Create a ViewGroup params,and set to the target view.
	 */
	public static void setViewGroupParams(View view, int width, int height) {
		if (view != null) {
			ViewGroup.LayoutParams params = view.getLayoutParams();
			if (params == null) {
				params = new ViewGroup.LayoutParams(width, height);
			} else {
				params.width = width;
				params.height = height;
			}

			view.setLayoutParams(params);
		}
	}

	/**
	 * Create a LinearLayout params,and set to the target view.
	 */
	public static void setLinearParams(View view, int width, int height) {
		if (view != null) {
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();

			if (params == null) {
				params = new LinearLayout.LayoutParams(width, height);
			} else {
				params.width = width;
				params.height = height;
			}
			view.setLayoutParams(params);
		}
	}

	/**
	 * Create a FrameLayout params,and set to the target view.
	 */
	public static void setFrameParams(View view, int width, int height) {
		if (view != null) {
			FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();

			if (params == null) {
				params = new FrameLayout.LayoutParams(width, height);
			} else {
				params.width = width;
				params.height = height;
			}
			view.setLayoutParams(params);
		}
	}

	/**
	 * Create a LinearLayout params with margins,and set to the target view.
	 */
	public static void setLinearParams(View view, int width, int height, int top, int left, int right, int bottom) {
		if (view != null) {
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
			if (params == null) {
				params = new LinearLayout.LayoutParams(width, height);
			} else {
				params.width = width;
				params.height = height;
			}

			params.topMargin = top;
			params.leftMargin = left;
			params.rightMargin = right;
			params.bottomMargin = bottom;

			view.setLayoutParams(params);
		}
	}

	/**
	 * Set layout params to a view
	 *
	 * @param view
	 * @param params
	 */
	public static void setLayoutParams(View view, ViewGroup.LayoutParams params) {
		if (view != null) {
			view.setLayoutParams(params);
		}
	}

	/**
	 * @param view
	 * @param gravity
	 */
	public static void setLinearGravity(View view, int gravity) {
		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
		if (layoutParams != null) {
			layoutParams.gravity = gravity;
			view.setLayoutParams(layoutParams);
		}
	}

	/**
	 * @param view
	 * @param gravity
	 */
	public static void setFrameGravity(View view, int gravity) {
		FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
		if (layoutParams != null) {
			layoutParams.gravity = gravity;
			view.setLayoutParams(layoutParams);
		}
	}

	/**
	 * @param view
	 * @param onClickListener
	 */
	public static void setClickListener(View view, View.OnClickListener onClickListener) {
		if (view != null) {
			view.setOnClickListener(onClickListener);
		}
	}

	/**
	 * @param view
	 * @param text
	 */
	public static void setText(TextView view, CharSequence text) {
		if (view != null) {
			view.setText(text);
		}
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setText(TextView view, int resId) {
		setText(view, ResourceUtils.getString(resId));
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setTextColorByResource(TextView view, int resId) {
		int color = ResourceUtils.getColor(resId);
		setTextColor(view, color);
	}

	/**
	 * @param view
	 * @param color
	 */
	public static void setTextColor(TextView view, int color) {
		if (view != null) {
			view.setTextColor(color);
		}
	}

	/**
	 * @param view
	 * @param text
	 */
	public static void setNonEmptyText(TextView view, CharSequence text) {
		if (view != null) {
			if (TextUtils.isEmpty(text)) {
				text = "";
			}
			view.setText(text);
		}
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setImageResource(ImageView view, int resId) {
		if (view != null) {
			view.setImageResource(resId);
		}
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setBackgroundResource(View view, int resId) {
		if (view != null) {
			int paddingLeft = view.getPaddingLeft();
			int paddingTop = view.getPaddingTop();
			int paddingRight = view.getPaddingRight();
			int paddingBottom = view.getPaddingBottom();

			view.setBackgroundResource(resId);
			view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
		}
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setBackgroundColor(View view, int resId) {
		if (view != null) {
			int paddingLeft = view.getPaddingLeft();
			int paddingTop = view.getPaddingTop();
			int paddingRight = view.getPaddingRight();
			int paddingBottom = view.getPaddingBottom();

			view.setBackgroundColor(resId);
			view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
		}
	}

	/**
	 * @param view
	 * @param enabled
	 */
	public static void setEnabled(View view, boolean enabled) {
		if (view != null) {
			view.setEnabled(enabled);
		}
	}

	/**
	 * @param view
	 * @param resId
	 */
	public static void setDrawableLeft(TextView view, int resId) {
		if (view != null) {
			Drawable drawableLeft = ResourceUtils.getDrawable(resId);
			drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
			view.setCompoundDrawables(drawableLeft, null, null, null);
		}
	}

	public static void setLinearWeight(View view, float weight) {
		if (view != null) {
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
			if (params == null) {
				return;
			}
			params.weight = weight;
			view.setLayoutParams(params);
		}
	}

	/**
	 * @param view
	 * @return
	 */
	public static boolean isTextViewEmpty(TextView view) {
		if (view != null) {
			return TextUtils.isEmpty(view.getText().toString().trim());
		}
		return true;
	}

	/**
	 * @param view
	 * @return
	 */
	public static String getTextViewText(TextView view) {
		if (view != null) {
			return view.getText().toString().trim();
		}
		return null;
	}

	/**
	 * View的getText()返回的CharSequence永远不为null，如有需要返回null，用这个.
	 *
	 * @param view
	 * @return
	 */
	public static String getTextViewText2(TextView view) {
		if (view != null) {
			String text = view.getText().toString().trim();
			return TextUtils.isEmpty(text) ? null : text;
		}
		return null;
	}



	/**
	 * 在TextView的文字后面继续添加文字
	 *
	 * @param view
	 * @param text
	 */
	public static void addToTextView(TextView view, String text) {
		if (view == null || text == null) {
			return;
		}
		String newText = getTextViewText(view) + text;
		view.setText(newText);
	}

	/**
	 * @param absListView
	 * @return
	 */
	public static boolean isLastItemVisible(AbsListView absListView) {
		int totalCount = absListView.getCount();
		return totalCount != 0 && absListView.getLastVisiblePosition() >= (totalCount - 1);
	}

	/**
	 * @param absListView
	 * @return
	 */
	public static boolean isLast3ItemVisible(AbsListView absListView) {
		int totalCount = absListView.getCount();
		if(totalCount!=0){
			if(totalCount>=3){
				return  absListView.getLastVisiblePosition() >= (totalCount - 3);
			}else{
				return  absListView.getLastVisiblePosition() >= (totalCount - 1);
			}
		}else{
			return false;
		}
	}

	/**
	 * 图片紧跟着文字显示
	 *
	 * @param context
	 * @param tv
	 * @param textResId
	 * @param imageResId
	 */
	public static void imageFollowText(Context context, TextView tv, int textResId, int imageResId) {
		String text = ResourceUtils.getString(textResId);
		SpannableStringBuilder builder = new SpannableStringBuilder(text);
		ImageSpan imageSpan = getImageSpanFollowText(context, imageResId);
		builder.setSpan(imageSpan, builder.length() - 1, builder.length(), 0);
		tv.setText(builder);
	}

	/**
	 * @param context
	 * @param imageResId
	 * @return
	 */
	public static ImageSpan getImageSpanFollowText(Context context, int imageResId) {
		Drawable d = ResourceUtils.getDrawable(imageResId);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
		return imageSpan;
	}

	/**
	 * 半角转全角,返回全角字符串
	 *
	 * @param content
	 * @return
	 */
	public static String setTextContent(String content) {
		char[] c = content.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}


	/**
	 * @param tv
	 */
	public static void restrictOneLineText(final TextView tv) {
		tv.post(new Runnable() {
			@Override
			public void run() {
				int lineCount = tv.getLineCount();
				if (tv.getEllipsize() == TextUtils.TruncateAt.END) {
					setLinearWeight(tv, 1f);
					tv.setMaxLines(1);
					return;
				}

				if (lineCount > 1) {
					setLinearWeight(tv, 1f);
					tv.setMaxLines(1);
					tv.setEllipsize(TextUtils.TruncateAt.END);
				} else {
					setLinearWeight(tv, 0f);
				}
			}
		});
	}

	/**
	 * @param tv
	 * @param text
	 */
	public static void restrictOneLineText(final TextView tv, String text) {
		tv.setText(text);
		restrictOneLineText(tv);
	}


	/**
	 * 设置列表项变化
	 * @param viewGroup：上层布局
	 */
	@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public static void setAllItem(Context context, ViewGroup viewGroup, int colorId) {
		if(viewGroup==null){
			return;
		}

		try {
			int count = viewGroup.getChildCount();
			for(int i=0; i<count; i++){
				View view = viewGroup.getChildAt(i);
				if (view instanceof ViewGroup) {
					//递归了。若是布局控件（LinearLayout或RelativeLayout）,继续查询子View
					setAllItem(context, (ViewGroup) view, colorId);
				}else if(view instanceof TextView){
					//判断是否为文本
					TextView newDtv = (TextView) view;
					newDtv.setTextColor(context.getResources().getColor(colorId));
				}else if(view instanceof RatingBar){
					//判断是否为星条
					RatingBar newDiv = (RatingBar) view;
					newDiv.setAlpha(0.7F);
				}
			}
		}catch (Exception ex){

		}
	}


}
